package com.qinsong.myapplication;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.hyphenate.EMContactListener;
import com.hyphenate.chat.EMClient;
import com.qinsong.myapplication.model.Model;
import com.qinsong.myapplication.model.bean.InvitationInfo;
import com.qinsong.myapplication.model.bean.UserInfo;
import com.qinsong.myapplication.utils.Constant;
import com.qinsong.myapplication.utils.SpUtils;

public class EventListener {
    private final LocalBroadcastManager mLBM;
    private Context mCoontext;


    public EventListener(Context context) {
        mCoontext = context;
        //创建一个发送广播的管理者对象
        mLBM = LocalBroadcastManager.getInstance(mCoontext);
       //注册u一个联系人变化的监听
        EMClient.getInstance().contactManager().setContactListener(emContactListener);
    }

    private final EMContactListener emContactListener = new EMContactListener() {
        /**
         * 联系人增加后执行
         * @param hxid
         */
        @Override
        public void onContactAdded(String hxid) {
            //数据库更新
            Model.getInstance().getDbManager().getContactDao().saveContact(new UserInfo(hxid),true);
            //发送联系人注册的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));
        }

        /**
         * 联系人删除后执行
         * @param hxid
         */
        @Override
        public void onContactDeleted(String hxid) {
            //数据库更新
            Model.getInstance().getDbManager().getContactDao().deleteContactByHxId(hxid);
            Model.getInstance().getDbManager().getInvitationDao().removeInvitation(hxid);
            //发送联系人注册的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));
        }

        /**
         * 联系人接受到新的邀请
         * @param hxid
         * @param reason
         */
        @Override
        public void onContactInvited(String hxid, String reason) {
            //数据库更新
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setUserInfo(new UserInfo(hxid));
            invitationInfo.setReason(reason);
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.NEW_INVITE);
            Model.getInstance().getDbManager().getInvitationDao().addInvitation(invitationInfo);
            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送联系人注册的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
        }

        /**
         * 别的联系人同意了你的好友邀请
         * @param hxid
         */
        @Override
        public void onFriendRequestAccepted(String hxid) {
            //数据库更新
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setUserInfo(new UserInfo(hxid));
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER);
            Model.getInstance().getDbManager().getInvitationDao().addInvitation(invitationInfo);
            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送联系人注册的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
        }

        /**
         * 别人拒绝了你的还有邀请
         * @param s
         */
        @Override
        public void onFriendRequestDeclined(String s) {
            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送联系人注册的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
        }
    };
}
